<?php
/*  2017年2月17日 星期五
 *  windows 系统服务
 *  Windows Management Instrumentation (WMI)   https://msdn.microsoft.com/en-us/library/aa393964(v=vs.85).aspx
 *  COM API for WMI   https://msdn.microsoft.com/en-us/library/aa389276(v=vs.85).aspx
 */
namespace hyang;
use COM;
class Win{
    private $_SWbemLocatorWmiObj;   //  SWbemLocator 对象
    public function isWin(){
        $win = PHP_OS;
        return substr_count(strtolower($win),'win') > 0? true:false;
    }
    public function SWbemLocator($osc,$filter=[])
    {
        if($this->_SWbemLocatorWmiObj) $wmi = $this->_SWbemLocatorWmiObj;
        else{
            $objLocator = new COM("WbemScripting.SWbemLocator");
            $wmi = $objLocator->ConnectServer();
            $this->_SWbemLocatorWmiObj = $wmi;
        }
        return $filter? $this->GetWMI($wmi,$osc,$filter):$this->GetWMI($wmi,$osc);
    }
    public function sys_windows() {
        $objLocator = new COM("WbemScripting.SWbemLocator");
        $wmi = $objLocator->ConnectServer();
        $prop = $wmi->get("Win32_PnPEntity");
        //CPU
        $cpuinfo = $this->GetWMI($wmi,"Win32_Processor", array("Name","L2CacheSize","NumberOfCores"));
        // debug($cpuinfo,true);

        // println($this->GetWMI($wmi,"Win32_Processor"));
        // println($this->GetWMI($wmi,"Win32_ComputerSystem"));
        // println($this->GetWMI($wmi,"Win32_OperatingSystem"));
        // println($this->GetWMI($wmi,"Win32_PageFileUsage"));
        // println($this->GetWMI($wmi,"Win32_PnPEntity"));
        // println($this->GetWMI($wmi,"Win32_COMApplication"));     // COM 应用
        // println($this->GetWMI($wmi,"Win32_Desktop"));               // Desktop
        // println($this->GetWMI($wmi,"Win32_UserDesktop"));               // UserDesktop
        // println($this->GetWMI($wmi,"Win32_TimeZone"));               // TimeZone
        // println($this->GetWMI($wmi,"Win32_SystemDriver"));               // SystemDriver
        // println($this->GetWMI($wmi,"Win32_Directory"));               // Directory
        // println($this->GetWMI($wmi,"Win32_Volume"));               // Volume
        // println($this->GetWMI($wmi,"Win32_NamedJobObjectLimit"));               // NamedJobObjectLimit
        // println($this->GetWMI($wmi,"Win32_ActiveRoute"));               // ActiveRoute
        // println($this->GetWMI($wmi,"Win32_IP4RouteTable"));               // IP4RouteTable
        // println($this->GetWMI($wmi,"Win32_NetworkConnection"));               // NetworkConnection
        // println($this->GetWMI($wmi,"Win32_PingStatus"));               // PingStatus
        // println($this->GetWMI($wmi,"Win32_SystemServices"));               // SystemServices 系统安装程序
        // println($this->GetWMI($wmi,"Win32_SystemDevices"));               // SystemDevices
        // println($this->GetWMI($wmi,"Win32_SystemUsers"));               // SystemUsers
        // println($this->GetWMI($wmi,"Win32_SystemDesktop"));               // SystemDesktop
        // println($this->GetWMI($wmi,"Win32_DependentService"));               // DependentService
        // println($this->GetWMI($wmi,"Win32_Process"));               // Process
        // println($this->GetWMI($wmi,"Win32_Registry"));               // Registry
        // println($this->GetWMI($wmi,"Win32_Service"));               // Service
        // println($this->GetWMI($wmi,"Win32_LogicalProgramGroup"));               // LogicalProgramGroup
        // println($this->GetWMI($wmi,"Win32_LogicalProgramGroup"));               // LogicalProgramGroup
        
        
        
        

        $res['CPU个数'] = $cpuinfo[0]['NumberOfCores'];
        if (null == $res['CPU个数']) {
        $res['CPU个数'] = 1;
        }
        for ($i=0;$i<$res['cpu']['num'];$i++){
        $res['CPU型号'] .= $cpuinfo[0]['Name']."<br>";
        $res['二级缓存'] .= $cpuinfo[0]['L2CacheSize']."<br>";
        }
        // SYSINFO
        $sysinfo = $this->GetWMI($wmi,"Win32_OperatingSystem", array('LastBootUpTime','TotalVisibleMemorySize','FreePhysicalMemory','Caption','CSDVersion','SerialNumber','InstallDate'));
        $res['操作系统版本'] = $sysinfo[0]['Caption']." ".$sysinfo[0]['CSDVersion'];
        $res['操作系统序列号'] = "{$sysinfo[0]['SerialNumber']} 于".date('Y年m月d日H:i:s',strtotime(substr($sysinfo[0]['InstallDate'],0,14)))."安装";
        //UPTIME
        $res['最后重启时间'] = $sysinfo[0]['LastBootUpTime'];
        
        
        $sys_ticks = 3600*8 + time() - strtotime(substr($res['最后重启时间'],0,14));
        $min = $sys_ticks / 60;
        $hours = $min / 60;
        $days = floor($hours / 24);
        $hours = floor($hours - ($days * 24));
        $min = floor($min - ($days * 60 * 24) - ($hours * 60));
        if ($days !== 0) $ress['day'] = $days."天";
        if ($hours !== 0) $ress['hours'] .= $hours."小时";
        $res['最后重启时间'] = $ress['day'].$ress['hours'].$min."分钟";
        
        //MEMORY
        $res['物理内存'] = $sysinfo[0]['TotalVisibleMemorySize'];
        $res['剩余内存'] = $sysinfo[0]['FreePhysicalMemory'];
        $res['已使用内存'] = $res['物理内存'] - $res['剩余内存'];
        $res['使用率'] = round($res['已使用内存'] / $res['物理内存']*100,2);
        
        $swapinfo = $this->GetWMI($wmi,"Win32_PageFileUsage", array('AllocatedBaseSize','CurrentUsage'));
        
        // TODO swp区获取
        $res['交换分区'] = $swapinfo[0][AllocatedBaseSize];
        $res['已经使用'] = $swapinfo[0][CurrentUsage];
        $res['剩余内存'] = $res['swapTotal'] - $res['swapUsed'];
        $res['使用率'] = (floatval($res['swapTotal'])!=0)?round($res['swapUsed']/$res['swapTotal']*100,2):0;
        
        // LoadPercentage
        $loadinfo = $this->GetWMI($wmi,"Win32_Processor", array("LoadPercentage"));
        $res['系统平均负载'] = $loadinfo[0]['LoadPercentage'];
        
        return $res;
    }
    
    public function GetWMI($wmi,$strClass, $strValue = array()) {
        $arrData = array();
        $objWEBM = $wmi->Get($strClass);
        $arrProp = $objWEBM->Properties_;
        $arrWEBMCol = $objWEBM->Instances_();
        foreach($arrWEBMCol as $objItem) {
            @reset($arrProp);
            $arrInstance = array();
            foreach($arrProp as $propItem) {
                eval("\$value = \$objItem->" . $propItem->Name . ";");
                if (empty($strValue)) {
                    $arrInstance[$propItem->Name] = @trim($value);
                } else {
                    if (in_array($propItem->Name, $strValue)) {
                        $arrInstance[$propItem->Name] = @trim($value);
                    }
                }
            }
            $arrData[] = $arrInstance;
        }
        return $arrData;
    } 
}